package cn.hfatec.shms.web.xc.xcJjbhz.action;

import cn.hfatec.shms.exception.NotAuthException;
import cn.hfatec.shms.system.user.model.User;
import cn.hfatec.shms.web.td.tdHzb.model.TdHzbParams;
import cn.hfatec.shms.web.xc.xcJjbmx.model.XcJjbmxParams;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.annotation.Resource;


import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.constants.SysContant;
import cn.hfatec.freemarker.FreeMarkerUtil;
import cn.hfatec.shms.web.xc.xcJjbhz.model.XcJjbhzParams;
import cn.hfatec.shms.web.xc.xcJjbhz.model.XcJjbhz;
import cn.hfatec.shms.web.xc.xcJjbhz.service.XcJjbhzService;
import cn.hfatec.shms.base.ShmsBaseAction;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;


/**
 * @author hfatec codegen
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Controller
@RequestMapping("/xc/xcJjbhz")
public class XcJjbhzAction extends ShmsBaseAction {

    //服务层
    @Resource(name = "XcJjbhzService")
    private XcJjbhzService xcJjbhzService;


    /**
     * 查询列表
     */
    @RequestMapping("/list")
    public String list(XcJjbhzParams xcJjbhzParams, Page page, Model model) {

        Table table = xcJjbhzService.queryXcJjbhzTable(xcJjbhzParams, page);
        XcJjbhz xcJjbhz=  xcJjbhzService.getXcJjbhzHj(xcJjbhzParams);
        if(xcJjbhz==null)
        {xcJjbhz=new XcJjbhz();}
        model.addAttribute("xcJjbhzsTable", table);
        model.addAttribute("xcJjbhzHj", xcJjbhz);
        model.addAttribute("xcJjbhzParams", xcJjbhzParams);
        return "/xc/xcJjbhz/list";
    }

    /**
     * 异步查询列表
     */
    @RequestMapping("/ajaxList")
    @ResponseBody
    public String ajaxList(XcJjbhzParams xcJjbhzParams, Page page, Model model) {
        try {
            list(xcJjbhzParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xc/xcJjbhz/table", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 跳转到新增页
     */
    @RequestMapping("/goAdd")
    @ResponseBody
    public String goAdd(Model model, HttpServletRequest request) {
        try {
            User user = (User) request.getSession().getAttribute(SysContant.LOGIN_USER_SESSION);
            if (user == null) {
                throw new NotAuthException("用户未授权");
            }
            XcJjbhz xcJjbhz = new XcJjbhz();
            Date dt = new Date();
            SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
            String date1 = date.format(dt);
            xcJjbhz.setUserName(user.getUserName());
            xcJjbhz.setEntryStart(date1);

            model.addAttribute("xcJjbhz", xcJjbhz);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xc/xcJjbhz/form1", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 跳转到编辑页
     */
    @RequestMapping("/goEdit")
    @ResponseBody
    public String goEdit(String id, Model model) {

        try {

            XcJjbhz xcJjbhz = new XcJjbhz();

            List xcJjbhzmx = xcJjbhzService.getXcJjbhzList(id);
            model.addAttribute("jjbhzmxList", xcJjbhzmx);
            model.addAttribute("xcJjbhz", xcJjbhz);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xc/xcJjbhz/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 根据IDs删除数据
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteXcJjbhz(String ids) {
        try {
            List<String> idList = new ArrayList<String>();
            if (ids.length() > 0) {
                idList = Arrays.asList(ids.split(","));
            }
            xcJjbhzService.deleteXcJjbhzBatch(idList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据" + ids + "删除异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.DELETE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, SysContant.DELETE_SUCCESS_MESSAGE).toString();
    }

    /**
     * 根据IDs删除数据
     */
    @RequestMapping("/deletes")
    @ResponseBody
    public String deleteXcJjbhz(Integer id) {
        try {
            xcJjbhzService.deleteXcJjbhz(id);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据" + id + "删除异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.DELETE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, SysContant.DELETE_SUCCESS_MESSAGE).toString();
    }

    /**
     * 保存数据
     */
    @RequestMapping("/save")
    @ResponseBody
    public String saveXcJjbhz(@Valid XcJjbhz xcJjbhz,BindingResult result, HttpServletRequest request) throws NotAuthException {

        try {
            User user = (User) request.getSession().getAttribute(SysContant.LOGIN_USER_SESSION);
            if (user == null) {
                throw new NotAuthException("用户未授权");
            }
            if (result.hasErrors()) {
                List<ObjectError> errors = result.getAllErrors();
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, errors.get(0).getDefaultMessage()).toString();
            }
            XcJjbhz xcJjbhzHd = xcJjbhzService.xcXcJjbhzHd(xcJjbhz.getEntryStart(), xcJjbhz.getUserName());
            if (xcJjbhzHd == null) {
                xcJjbhzService.saveXcJjbhz(xcJjbhz,user);
            } else {
                logger.error("该条数据已存在,请直接编辑！");
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE,"该条数据已存在,请直接编辑！").toString();

            }

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据XcJjbhz保存异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.SAVE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, SysContant.SAVE_SUCCESS_MESSAGE).toString();
    }


    /**
     * 保存数据
     */
    @RequestMapping("/saves")
    @ResponseBody
    public String saveXcJjbhzs(String[] ids, String[] userName, String[] jkxjJj, String[] jkxjYj, String[] tjJj, String[] tjGj,String[] ALp, String[] BLp,String[] CLp, @Valid XcJjbhz xcJjbhz, BindingResult result) {

        try {
            if (result.hasErrors()) {
                List<ObjectError> errors = result.getAllErrors();
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, errors.get(0).getDefaultMessage()).toString();
            }
            xcJjbhzService.saveXcJjbhzs(xcJjbhz, ids, userName, jkxjJj, jkxjYj, tjJj, tjGj,ALp,BLp, CLp);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据XcJjbhz保存异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.SAVE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, SysContant.SAVE_SUCCESS_MESSAGE).toString();
    }

    /**
     * 导出excel
     */
    @RequestMapping("/export")
    @ResponseBody
    public String export(XcJjbhzParams xcJjbhzParams, HttpServletRequest request, HttpServletResponse response) {
        try {
            xcJjbhzService.exportExcel(xcJjbhzParams, request, response);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }
}
